package com.jhgsys.internal.formDesign.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.formDesign.dto.FormDesignDto;
import com.jhgsys.internal.formDesign.dto.FormFillDto;
import com.jhgsys.internal.formDesign.entity.FormFill;
import com.jhgsys.internal.formDesign.service.IFormDesignService;
import com.jhgsys.internal.formDesign.service.IFormFillService;
import com.jhgsys.internal.system.entity.User;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 表单填报 Controller
 *
 * @author zhangwei
 * @date 2024-09-05 08:53:07
 */
@Slf4j
@Validated
@Controller
public class FormFillController extends BaseController {

    @Autowired
    private IFormFillService formFillService;

    @Autowired
    private IFormDesignService formDesignService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "formFill")
        @RequiresPermissions("formFill:view")
    public String formFillIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("formDesign/formFill/formFillList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "formFillRecord")
    @RequiresPermissions("formFill:view")
    public String formFillRecordIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("formDesign/formFill/formFillRecord");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "formFill/detail/{id}")
    @RequiresPermissions("formFill:view")
    public String formFillDetailIndex(Model model, @PathVariable("id") String id){
        model.addAttribute("formFillData", formFillService.findFormFillById(id));
        return FebsUtil.view("formDesign/formFill/formFillDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "formFill/add/{id}")
    @RequiresPermissions("formFill:add")
    public String formFillAdd(Model model, @PathVariable("id") String id){
        model.addAttribute("formFillData", formDesignService.findFormDesignById(id));
        return FebsUtil.view("formDesign/formFill/formFillEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "formFill/update/{id}")
    @RequiresPermissions("formFill:edit")
    public String formFillUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("formFillData", formFillService.findFormFillById(id));
        return FebsUtil.view("formDesign/formFill/formFillEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-05 08:53:07
     */
    @GetMapping("formFill")
    @ResponseBody
    @RequiresPermissions("formFill:view")
    public FebsResponse getAllFormFills(FormFill formFill) {
        return new FebsResponse().success().data(formFillService.findFormFillList(formFill));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-05 08:53:07
     */
    @GetMapping("formFill/list")
    @ResponseBody
    @RequiresPermissions("formFill:view")
    public FebsResponse formFillList(QueryRequest request, FormFill formFill) {
        Map<String, Object> dataTable = getDataTable(this.formFillService.findFormFillPage(request, formFill));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 查询历史录入（分页）
     * @author zhangwei
     * @date 2024-09-05 08:53:07
     */
    @GetMapping("formFillRecord/list")
    @ResponseBody
    @RequiresPermissions("formFill:view")
    public FebsResponse formFillRecordList(QueryRequest request, FormFill formFill) {
        User currentUser = getCurrentUser();
        formFill.setUserId(currentUser.getUserId());
        Map<String, Object> dataTable = getDataTable(this.formFillService.findFormFillRecordPage(request, formFill));
        return new FebsResponse().success().data(dataTable);
    }
    /**
     * 新增 FormFill
     * @author zhangwei
     * @date 2024-09-05 08:53:07
     */
    @Log("新增FormFill")
    @PostMapping("formFill")
    @ResponseBody
    @RequiresPermissions("formFill:add")
    public FebsResponse addFormFill(@RequestBody @Valid FormFillDto formFillDto) throws FebsException {
        try {
            formFillDto.setCreatedBy(getCurrentUser().getUserName());
            formFillDto.setUserId(getCurrentUser().getUserId());
            formFillDto.setUserNo(getCurrentUser().getUserNo());
            formFillDto.setDeptId(getCurrentUser().getDeptId());
            formFillDto.setDeptName(getCurrentUser().getDeptName());
            formFillDto.setRealName(getCurrentUser().getUname());
            formFillDto.setFillDate(new Date());
            this.formFillService.createFormFill(formFillDto);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增FormFill失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
    /**
     * 修改 FormFill
     * @author zhangwei
     * @date 2024-09-05 08:53:07
     */
    @Log("修改FormFill")
    @PostMapping("formFill/update")
    @ResponseBody
    @RequiresPermissions("formFill:edit")
    public FebsResponse updateFormFill(@RequestBody @Valid FormFillDto formFillDto) throws FebsException {
        try {
            formFillDto.setUpdatedBy(getCurrentUser().getUserName());
            this.formFillService.updateFormFill(formFillDto);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改FormFill失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
    /**
     * 逻辑删除 FormFill
     * @author zhangwei
     * @date 2024-09-05 08:53:07
     */
    @Log("逻辑删除FormFill")
    @GetMapping("formFill/delete/{id}")
    @ResponseBody
    @RequiresPermissions("formFill:delete")
    public FebsResponse deleteFormFill(@PathVariable("id") String formFillIds) throws FebsException {
        try {
            FormFill formFill = new FormFill();
            formFill.setDeletedBy(getCurrentUser().getUserName());
            formFill.setDeletedTime(new Date());
            formFill.setDeletedFlg(FebsConstant.INVALID);
            this.formFillService.deleteFormFill(formFill,formFillIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除FormFill失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 FormFill
    * @author zhangwei
    * @date 2024-09-05 08:53:07
    */
    @Log("物理删除FormFill")
    @GetMapping("formFill/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("formFill:delete")
    public FebsResponse physicallyDeleteFormFill(@PathVariable("id") String formFillIds) throws FebsException {
        try {
            this.formFillService.physicallyDeleteFormFill(formFillIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除FormFill失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }


}
